﻿@page "/master-detail"

@using RadzenBlazorDemos.Data
@using RadzenBlazorDemos.Models.Northwind
@using Microsoft.EntityFrameworkCore

@inject NorthwindContext dbContext

<h1>Master/Detail</h1>

<p>This page demonstrates how to create a master/detail relationship between two Radzen Blazor DataGrid components.</p>

<RadzenExample Name="MasterDetail" Heading="false" Documentation="false">
@if (orders == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <div class="row">
        <div class="col-md-6">
            <RadzenDataGrid ColumnWidth="200px" AllowFiltering="true" AllowPaging="true" PageSize="3" AllowSorting="true" Data="@orders" TItem="Order" @bind-Value="@SelectedOrders">
                <Columns>
                    <RadzenDataGridColumn Width="100px" TItem="Order" Property="OrderID" Title="Order ID" />
                    <RadzenDataGridColumn Width="200px" TItem="Order" Property="Customer.CompanyName" Title="Customer" />
                    <RadzenDataGridColumn Width="150px" TItem="Order" Property="Employee.LastName" Title="Employee">
                        <Template Context="order">
                            <RadzenImage Path="@order.Employee?.Photo" style="width: 40px; height: 40px; border-radius: 8px;" />
                            @order.Employee?.FirstName @order.Employee?.LastName
                        </Template>
                    </RadzenDataGridColumn>
                    <RadzenDataGridColumn TItem="Order" Property="OrderDate" Title="Order Date" FormatString="{0:d}" />
                    <RadzenDataGridColumn TItem="Order" Property="RequiredDate" Title="Required Date" FormatString="{0:d}" />
                    <RadzenDataGridColumn TItem="Order" Property="ShippedDate" Title="Shipped Date" FormatString="{0:d}" />
                    <RadzenDataGridColumn TItem="Order" Property="ShipName" Title="Ship Name" />
                    <RadzenDataGridColumn TItem="Order" Property="ShipCountry" Title="Ship Country" />
                </Columns>
            </RadzenDataGrid>
        </div>
        <div class="col-md-6">
            <RadzenCard Style="margin-bottom:20px">
                Company:
                <b>@SelectedOrders.FirstOrDefault()?.Customer?.CompanyName</b>
            </RadzenCard>
            <RadzenTabs>
                <Tabs>
                    <RadzenTabsItem Text="Order Details">
                        <RadzenDataGrid AllowFiltering="true" AllowPaging="true" AllowSorting="true" Data="@(SelectedOrders.FirstOrDefault()?.OrderDetails)" TItem="OrderDetail">
                            <Columns>
                                <RadzenDataGridColumn TItem="OrderDetail" Property="Order.CustomerID" Title="Order" />
                                <RadzenDataGridColumn TItem="OrderDetail" Property="Product.ProductName" Title="Product" />
                                <RadzenDataGridColumn TItem="OrderDetail" Property="UnitPrice" Title="Unit Price">
                                    <Template Context="detail">
                                        @String.Format(new System.Globalization.CultureInfo("en-US"), "{0:C}", detail.UnitPrice)
                                    </Template>
                                </RadzenDataGridColumn>
                                <RadzenDataGridColumn TItem="OrderDetail" Property="Quantity" Title="Quantity" />
                                <RadzenDataGridColumn TItem="OrderDetail" Property="Discount" Title="Discount">
                                    <Template Context="detail">
                                        @String.Format("{0}%", detail.Discount * 100)
                                    </Template>
                                </RadzenDataGridColumn>
                            </Columns>
                        </RadzenDataGrid>
                    </RadzenTabsItem>
                    <RadzenTabsItem Text="Products">
                        <RadzenDataList WrapItems="true" AllowPaging="true" Data="@(SelectedOrders.FirstOrDefault()?.OrderDetails)" TItem="OrderDetail" PageSize="10">
                            <Template Context="detail">
                                <RadzenCard Style="width:100px;height:100px">
                                    Product:
                                    <b>@detail?.Product?.ProductName</b>
                                </RadzenCard>
                            </Template>
                        </RadzenDataList>
                    </RadzenTabsItem>
                </Tabs>
            </RadzenTabs>
        </div>
    </div>
}
</RadzenExample>

@code {
    IList<Order> SelectedOrders { get; set; }

    IQueryable<Order> orders;

    protected override void OnInitialized()
    {
        orders = dbContext.Orders
            .Include("Customer")
            .Include("Employee")
            .Include("OrderDetails")
            .Include("OrderDetails.Product");

        SelectedOrders = new List<Order>(){ orders.FirstOrDefault() };
    }
}
